package edu.neusoft.yiqibike.common.entity.convert;

import edu.neusoft.yiqibike.common.entity.mysql.Permission;
import edu.neusoft.yiqibike.common.entity.vo.AuthoRangeVo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * User：lyc
 * Date：2017-12-31
 * Time：15:54
 * Description：
 */
public class PermissionRangeConverter extends Converter<AuthoRangeVo, Permission> {
    public PermissionRangeConverter(final Integer level, final List<String> permissionCodes) {
        super(permission -> {
            AuthoRangeVo permissionRangeVo = new AuthoRangeVo();
            permissionRangeVo.setKey(permission.getId());
            permissionRangeVo.setLabel(permission.getCode());
            permissionRangeVo.setType("permission");
            permissionRangeVo.setDisabled(true);
            permissionRangeVo.setPinyin(permission.getCode());
            if (Objects.nonNull(level)) {
                boolean isAllow = true;
                if (Integer.compare(level, permission.getLevel()) < 0) {
                    //如果管理员级别小于当前权限的级别但是该管理员拥有当前权限则管理员可对当前权限进行操作
                    if (!CollectionUtils.isEmpty(permissionCodes) && permissionCodes.contains(permission.getCode())) {
                        isAllow = false;
                    }
                } else {
                    //如果管理员级别大于当前权限的级别则可对当前权限进行操作
                    isAllow = false;
                }
                permissionRangeVo.setDisabled(isAllow);
            }
            return permissionRangeVo;
        }, vo -> new Permission(vo.getKey(), vo.getLabel()));
    }
}
